package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.constants.StatusConstants;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
/**
 * @author 45505
 */
@Service
@Slf4j
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Resource
    private IWorkerCertificationService workerCertificationService;
    @Override
    public PageResult<WorkerCertificationAudit> pageQuery(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryDTO) {
        if (Objects.isNull(workerCertificationAuditPageQueryDTO)) {
            throw new BadRequestException("分页信息不能为空");
        }
        Page<WorkerCertificationAudit> page = PageUtils
                .parsePageQuery(workerCertificationAuditPageQueryDTO, WorkerCertificationAudit.class);
        LambdaQueryWrapper<WorkerCertificationAudit> eq = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryDTO.getName()),WorkerCertificationAudit::getIdCardNo, workerCertificationAuditPageQueryDTO.getIdCardNo())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryDTO.getName()),WorkerCertificationAudit::getName, workerCertificationAuditPageQueryDTO.getName())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryDTO.getName()),WorkerCertificationAudit::getAuditStatus, workerCertificationAuditPageQueryDTO.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(workerCertificationAuditPageQueryDTO.getName()),WorkerCertificationAudit::getCertificationStatus, workerCertificationAuditPageQueryDTO.getCertificationStatus());
        Page<WorkerCertificationAudit> workerCertificationAuditPage = baseMapper.selectPage(page, eq);
        return PageUtils.toPage(workerCertificationAuditPage,WorkerCertificationAudit.class);
    }

    @Override
    public void auditById(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        if (Objects.isNull(id) || Objects.isNull(certificationAuditReqDTO)){
            throw new BadRequestException("信息为空");
        }
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        WorkerCertificationAudit certificationAudit = getById(id);
        certificationAudit.setAuditStatus(StatusConstants.ONE);
        certificationAudit.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        certificationAudit.setAuditorId(currentUserInfo.getId());
        certificationAudit.setAuditTime(new Date());
        certificationAudit.setAuditorName(currentUserInfo.getName());
        WorkerCertification workerCertification = workerCertificationService.getById(certificationAudit.getServeProviderId());
        workerCertification.setCertificationStatus(certificationAuditReqDTO.getCertificationStatus());
        workerCertification.setCertificationTime(LocalDateTime.now());
        if (certificationAuditReqDTO.getCertificationStatus().equals(StatusConstants.THREE)){
            certificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
            updateById(certificationAudit);
            workerCertificationService.updateById(workerCertification);
        }else {
            updateById(certificationAudit);
            workerCertificationService.updateById(workerCertification);
        }
    }

    @Override
    public void workerCertificationAudit(WorkerCertificationAuditAddReqDTO workerCertificationAuditAddReqDTO) {
        if (Objects.isNull(workerCertificationAuditAddReqDTO)) {
            throw new BadRequestException("认证信息不能为空");
        }
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        log.info("{}",currentUserInfo);
        WorkerCertificationAudit workerCertificationAudit = BeanUtils.toBean(workerCertificationAuditAddReqDTO, WorkerCertificationAudit.class);
        workerCertificationAudit.setServeProviderId(currentUserInfo.getId());
        save(workerCertificationAudit);
        WorkerCertification workerCertification = BeanUtils.toBean(workerCertificationAudit, WorkerCertification.class);
        workerCertification.setId(currentUserInfo.getId());
        workerCertificationService.save(workerCertification);

    }
}
